package com.surrey.p2p.model;

import java.util.Observable;

/**
 * Base class to represent a generic node in a Peer to Peer network. Not
 * specific to any particular algorithm.
 * 
 * @author Vedika Dalmia
 */
public class P2PNode extends Observable {

	private Address address;
	private boolean online;
	private boolean connected;

	/**
	 * Create new node with a given address.
	 * 
	 * @param String
	 *            IPAddress or some unique identifier for the node.
	 */
	public P2PNode(String ipAddress) {
		this.address = new Address(ipAddress);
		connected = false;
		online = false;
	}

	/**
	 * Set the node to be live No effect if node is already live
	 * 
	 */
	public void makeLive() {
		if (this.online == false) {
			this.online = true;
			this.setChanged();
			this.notifyObservers("Online");
		}
	}
	
	public void takeOffline(){
		if (this.online == true) {
			this.online = false;
			this.setChanged();
			this.notifyObservers("Offline");
		}
	}

	/**
	 * Connects the node to the peer to peer network
	 * No effect if node is already connected
	 */
	public void joinNetwork() {
		if(this.connected==false){
			this.connected = true;
			this.setChanged();
			this.notifyObservers("Connected");
		}
	}

	/**
	 * Get the Address of this node
	 * 
	 * @return Address of this node
	 */
	public Address getAddress() {
		return address;
	}

	public boolean isOnline(){
		return this.online;
	}
	
	@Override
	public String toString() {
		return address.toString() ;
	}

	

}
